#ifndef COPIERHANDLER_H
#define COPIERHANDLER_H

#include <QObject>
#include "foldercopierthread.h"
#include "discdata.h"
#include "QNetworkReply"
#include "cfgfile.h"
#include "pelicanosws.h"
#include "makemkvinfo.h"

class CopierHandler : public QObject
{
    Q_OBJECT
public:
    explicit CopierHandler(QString letter, const char *source,  QString to,QString finalPath,QString procesName, QObject *parent= 0);
    ~CopierHandler();
    void processRAR();
    void processPAR2();
    void processYENC();
    void processNZB();
    void createRAR(QString path, QString fileName);
    void createPAR2(QString path, QString fileName);
    void createYENC(QString path, QString fileName,bool);
    void createUPLOADNZB(QString path, QString fileName);

    void startCopyDisc(QString path, QString fileName);
    void prepareFilesAndFolders();

    void stopCopyDisc();
    void setDiscData(DiscData discData);

    void setLabel(QString label);
    void setFileName(QString &name);
    void setDestinationPath(QString &value);
    void setIdWs(QString &id);

    QString getIdWs();

    QString getDestinationPath();

signals:
    void finished(QString id,QString path,QString discId,bool parentalControl);
    void canceled(QString id,QString path,QString discId,bool parentalControl);
    void readyToCopy();
    void updateProgress(int value);
    void sendProgress(int value);
    void step();

public slots:
    //void uploadNzbToServer();
    //void createPar2(QString id,QString path,QString discId,bool parentalControl);
    //void createAndUploadNzb(QString id,QString path,QString discId,bool parentalControl);
    //stepping
    void startMakeMKV();
    void startRippingMakeMKV();
    void start7ZIP();
    void create7ZIP();
    void startRAR();
    void startPAR2();
    void startYENC(bool retry=false);
    void startUPLOADNZB();
    void cleanFolder(QString path);
    void removeRARs();
    void removePAR2s();
    void ejectDisc();
    void copyCanceled(QString id, QString path, QString discId, bool parentalControl);
    void copyDisc();
    void setDiscDataList(QList<DiscData> discData);
    void checkProgress();
    void changeLetter(QString);
    void changeTempFolder(QString path);
    void changeFinalFolder(QString path);

    void replyFinished(QNetworkReply *);

    void processFinished(int exitCode,QProcess::ExitStatus exitStatus);
    void readyReadStandardOutput();
    void error(QProcess::ProcessError);

    void processFinishedMkv(int exitCode,QProcess::ExitStatus exitStatus);
    void readyReadStandardOutputMkv();
    void errorMkv(QProcess::ProcessError);

    void processFinishedRippingMkv(int exitCode,QProcess::ExitStatus exitStatus);
    void readyReadStandardOutputRippingMkv();
    void errorRippingMkv(QProcess::ProcessError);

    void processFinished7z(int exitCode,QProcess::ExitStatus exitStatus);
    void readyReadStandardOutput7z();
    void error7z(QProcess::ProcessError);

    void processFinishedPar2(int exitCode,QProcess::ExitStatus exitStatus);
    void readyReadStandardOutputPar2();
    void errorPar2(QProcess::ProcessError);


    void processFinishedNzb(int exitCode,QProcess::ExitStatus exitStatus);
    void readyReadStandardOutputNzb();
    void errorNzb(QProcess::ProcessError);

    bool isRunning(const QString &process);

private:
    FolderCopierThread *_copier;
    QTimer  timer;
    QList<DiscData> _discDataList;
    DiscData _discData;
    QString _driveLetter;
    QString _destinationPath;
    QString _tempPath;
    CFGFile _cfgFile;
    //
    QString _id;
    QString _discId;
    QString _label;
    bool _parentalControl;
    QString _fileName;
    QString _idWS;

    QString getM(const QString &fileName);
    QString getM59(const QString &fileName);
    QString extraValue(int qty, char asciiValue);
    QNetworkReply* _reply;
    PelicanoSWS _pelicanosws;
    QString _procesName;
    MakemkvInfo _makeInfo;
    bool _error;
};

#endif // COPIERHANDLER_H
